package com.becca.cake.controller.sms;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.becca.cake.core.common.BaseAction;
import com.becca.cake.core.common.PagingBean;
import com.becca.cake.core.exception.CommonExceptionHandle;
import com.becca.cake.model.sms.SmsUserModel;
import com.becca.cake.service.sms.ISmsService;
import com.becca.cake.util.JsonUtil;

/**
 * 短信管理控制层
 * @copyright  尊星(上海)信息科技有限公司
 * @author hejinguo
 * @date 2016年9月2日 下午12:02:33
 */
@Controller
@RequestMapping("/smsManager")
public class SmsAction extends BaseAction {
    private static final Logger logger = LoggerFactory.getLogger(SmsAction.class);
    @Autowired
    private ISmsService         iSmsService;

    /**
     * 短信查询
     * @param subjectModel
     * @return
     */
    @RequestMapping(value = "/selectAllSms", method = RequestMethod.POST)
    @ResponseBody
    public String selectAllSms(SmsUserModel smsUserModel) {
        PagingBean page = getInitPagingBean();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("smsUserModel", smsUserModel);
        map.put("pagingBean", page);
        try {
            // step1:获取全部短信数据
            List<SmsUserModel> list = this.iSmsService.selectAllSms(map);
            // step2:获取全部短信总条数
            Long count = this.iSmsService.selectAllSmsCount(map);
            return JsonUtil.writeObjectPageJSON(list, count);
        } catch (Exception e) {
            logger.error("查询短信信息失败!", e);
            return JsonUtil.returnJsonUnSuccess("查询短信信息失败!");
        }
    }

    /**
     * 导入用户手机号
     * @author hejinguo
     * @param request
     * @return
     */
    @RequestMapping(value = "/importExcelUserPhone", method = RequestMethod.POST)
    public @ResponseBody
    String importExcelUserPhone(HttpServletRequest request, HttpServletResponse response) {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile files = multipartRequest.getFile("phoneNumber");
        String result = "";
        try {
            result = this.iSmsService.importExcelUserPhone(files);
        } catch (Exception e) {
            logger.error("导入用户手机号失败", e);
            return JsonUtil.returnJsonUnSuccess("导入用户手机号失败");
        }
        return result;
    }

    /**
     * 发送短信息
     * @author hejinguo
     * @param request
     * @return
     */
    @RequestMapping(value = "/addSendSms", method = RequestMethod.POST)
    public @ResponseBody
    String addSendSms(HttpServletRequest request, HttpServletResponse response,
                      SmsUserModel smsUserModel) {
        try {
            this.iSmsService.addSendSms(smsUserModel, this.getUserSeesion().getTrueName());
            return JsonUtil.returnJsonSuccess("短信发送成功");
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

}
